package com.yalemang.photovalut.ui.calculator

import UserManager
import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.yalemang.photovalut.R
import com.yalemang.photovalut.ui.calculator.adapter.SkinAdapter
import com.yalemang.photovalut.base.BaseSkinActivity
import com.yalemang.photovalut.base.user.Skin
import com.yalemang.photovalut.base.widget.CalculatorTextView
import com.yalemang.photovalut.databinding.ActivityCalculatorBinding
import com.yalemang.photovalut.ui.main.MainActivity


class CalculatorActivity : BaseSkinActivity<ActivityCalculatorBinding, CalculatorPresenter>(), CalculatorContract.CalculatorView {

    private lateinit var mSkinAdapter : SkinAdapter

    private var mainColorViews : ArrayList<View> = ArrayList()
    private var viceColorViews : ArrayList<View> = ArrayList()

    override fun initData() {

    }

    override fun initView() {
        mainColorViews.add(mBinding.tvCalculator0)
        mainColorViews.add(mBinding.tvCalculator1)
        mainColorViews.add(mBinding.tvCalculator2)
        mainColorViews.add(mBinding.tvCalculator3)
        mainColorViews.add(mBinding.tvCalculator4)
        mainColorViews.add(mBinding.tvCalculator5)
        mainColorViews.add(mBinding.tvCalculator6)
        mainColorViews.add(mBinding.tvCalculator7)
        mainColorViews.add(mBinding.tvCalculator8)
        mainColorViews.add(mBinding.tvCalculator9)
        mainColorViews.add(mBinding.tvXxx)
        viceColorViews.add(mBinding.tvCalculatorAdd)
        viceColorViews.add(mBinding.tvCalculatorSubtract)
        viceColorViews.add(mBinding.tvCalculatorMultiply)
        viceColorViews.add(mBinding.tvCalculatorDivide)
        viceColorViews.add(mBinding.tvCalculatorEqual)
        mainColorViews.add(mBinding.tvCalculatorAC)
        mainColorViews.add(mBinding.tvCalculatorDEL)
        mainColorViews.add(mBinding.tvCalculatorPercentage)
        mainColorViews.add(mBinding.tvCalculatorDecimalPoint)

        mBinding.ivCalculatorSkin.setOnClickListener{
            mBinding.drawerLayout.openDrawer(GravityCompat.END)
        }

        val toggle = ActionBarDrawerToggle(
            this,
            mBinding.drawerLayout,
            R.string.str_navigation_drawer_open,
            R.string.str_navigation_drawer_close
        )
        mBinding.drawerLayout.addDrawerListener(toggle)
        toggle.syncState()

        val linearLayoutManager = LinearLayoutManager(this)
        mSkinAdapter = SkinAdapter(UserManager.instance.getSkinData())
        mBinding.rvSkin.layoutManager = linearLayoutManager
        mBinding.rvSkin.adapter = mSkinAdapter

        mBinding.tvCalculatorDEL.setOnClickListener{
            var string = ""
            for (i in 0..<mBinding.etCalculator.text.length - 1){
                string += mBinding.etCalculator.text[i]
        }
            mBinding.etCalculator.setText(string)
        }
    }

    override fun switchSkin(skin: Skin){
        (0..<mainColorViews.size).forEach{
            val view = mainColorViews[it]
            if (view is CalculatorTextView){
                view.defaultColor = skin.mainColor
            }
            view.setBackgroundColor(skin.mainColor)
        }

        (0..<viceColorViews.size).forEach{
            val view = viceColorViews[it]
            if (view is CalculatorTextView){
                view.defaultColor = skin.viceColor
            }
            view.setBackgroundColor(skin.viceColor)
        }
    }

    @SuppressLint("SetTextI18n")
    fun click(view: View) {
        val tv: TextView = view as TextView
        if (tv.text.length > 1) {

        } else if (tv.text.length == 1) {
            val c = tv.text[0]
            if (c in '0'..'9') {
                var s: String = mBinding.etCalculator.text.toString()
                mBinding.etCalculator.setText(s + c)
            } else if (c == '.') {

            } else if (c == '=') {
                val password = mBinding.etCalculator.text.toString()
                if (mPresenter.mModel.isPasswordOK(password)) {
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)
                    finish()
                } else {
                    Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show()
                }
            } else {

            }
        } else {

        }
    }

    override fun initMVP() {
        mPresenter = CalculatorPresenter()
        mPresenter.mView = this
        mPresenter.mModel = CalculatorModel()
    }
}